<?php
class C_users extends CI_Controller {
	function __construct(){
		parent::__construct();
		checkLogin();
		$this->load->model('m_users');
	}
	function index(){
		$task = secure_post('task');
		switch($task){
			case 'LIST' :
				$this->_list();
				break;
			case 'INSERT' :
				$this->_insert();
				break;
			case 'UPDATE' :
				$this->_update();
				break;
			case 'DELETE' :
				$this->_delete();
				break;
			default :
				$data["s_usergroups"] = $this->m_users->s_usergroups();
				$this->load->view('v_users',$data);
		}
	}
	
	function _list(){
		$data['page'] = numeric_post("page");
		$data['rows'] = numeric_post("rows");
		$data['user_name'] = nosql_post("user_name");
		echo $this->m_users->_list($data);
	}
	
	function _insert(){
		$data = array(
			'user_name' => $this->input->post('user_name'), 
			'user_groups' => $this->input->post('user_group'), 
			'user_passwd' => md5($this->input->post('user_passwd')), 
			  
			'users_sys_active'=> '1',
			'users_sys_owner'	=>$_SESSION[SESSION_USERID]);
		$this->m_users->_insert($data);
		echo 1;
	}
	
	function _update(){
		$user_id = $this->input->post('user_id');
		$data = array(
			'user_name' => $this->input->post('user_name'), 
			'user_groups' => $this->input->post('user_group'), 
			
			'users_sys_owner'	=> $_SESSION[SESSION_USERID],
			'users_sys_update'=> date("Y-m-d h:i:s"));
		
		$user_passwd = $this->input->post('user_passwd'); 
		
		if ($user_passwd != "")
			$data["user_passwd"] = md5($user_passwd);
			
		$this->m_users->_update($data,$user_id);
		echo 1;
 	}
	
	function _delete(){
		$user_id= $this->input->post('user_id');
		$data = array(
			'users_sys_active' => '0',
			'users_sys_owner'	=> $_SESSION[SESSION_USERID],
			'users_sys_delete'=> date("Y-m-d h:i:s")
		);
		$this->m_users->_delete($data,$user_id);
		echo 1;
	}
}